﻿using Code;
using FluorineFx.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Parse
{
    /// <summary>
    /// 反序列化JSON  代码
    /// </summary>
    public class ParseCode
    {
        /// <summary>
        /// 循环次数
        /// </summary>
        private static int forInt = 100000;

        /// <summary>
        /// NET系统类 反序列化 实体类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="JsonStr"></param>
        /// <returns></returns>
        public static T DataContractModelJson<T>(string JsonStr)
        {
            object jsonObj=null;
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                jsonObj = JsonHelp.JsonToObject<T>(JsonStr);
            }
            timer.Stop();
            Console.WriteLine("内存使用:" + Help.BytesCountToString(Environment.WorkingSet) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return (T)jsonObj;
        }

        /// <summary>
        /// NET系统类 反序列化 实体类 加速
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static T DataContractModelJsonSpeed<T>(string JsonStr)
        {
            object jsonObj = null;
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                jsonObj = JsonHelp.JsonToModel<T>(JsonStr);
            }
            timer.Stop();
            Console.WriteLine("内存使用:" + Help.BytesCountToString(Environment.WorkingSet) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return (T)jsonObj;
        }

        /// <summary>
        /// NET WebJavaScript系统类 反序列化 实体类 加速
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static T WebJavaScriptModelJsonSpeed<T>(string JsonStr)
        {
            object jsonObj = null;
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                jsonObj = JsonHelp.JsonToJavaScriptModel<T>(JsonStr);
            }
            timer.Stop();
            Console.WriteLine("内存使用:" + Help.BytesCountToString(Environment.WorkingSet) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return (T)jsonObj;
        }


        /// <summary>
        /// Newtonsoft.Json 反序列化 实体类
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static T NewtonsoftModelJson<T>(string JsonStr)
        {
            object jsonObj = null;
            Newtonsoft.Json.JsonSerializerSettings jsetting = new Newtonsoft.Json.JsonSerializerSettings()
            {
                NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
                Formatting = Newtonsoft.Json.Formatting.None,
            };
            jsetting.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-ddTHH:mm:ss" });

            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(JsonStr, jsetting);
            }
            timer.Stop();
            Console.WriteLine("内存使用:" + Help.BytesCountToString(Environment.WorkingSet) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return (T)jsonObj;
        }



        /// <summary>
        /// FluorineFx 反序列化 Dictionary
        /// </summary>
        /// <param name="JsonStr"></param>
        /// <param name="timer"></param>
        /// <param name="model"></param>
        public static T FluorineFxModelJson<T>(string JsonStr)
        {
            object jsonObj = null;
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                string JsonString = Regex.Replace(JsonStr, "\"\\\\/Date\\(\\d{13}\\)\\\\/\"", match =>
                {

                    return "new " + match.Groups[0].Value.Replace("\\/", "").Replace("\"", "");
                });
                jsonObj = JavaScriptConvert.DeserializeObject<T>(JsonString);
            }
            timer.Stop();
            Console.WriteLine("内存使用:" + Help.BytesCountToString(Environment.WorkingSet) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return (T)jsonObj;
        }

        /// <summary>
        /// FluorineFx 反序列化 Dictionary
        /// </summary>
        /// <param name="JsonStr"></param>
        /// <param name="timer"></param>
        /// <param name="model"></param>
        public static T FluorineFxDictionaryJson<T>(string JsonStr)
        {
            object jsonObj = null;
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                jsonObj = JavaScriptConvert.DeserializeObject(JsonStr);
            }
            timer.Stop();
            Console.WriteLine("内存使用:" + Help.BytesCountToString(Environment.WorkingSet) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return (T)jsonObj;
        }







        /// <summary>
        /// FluorineFx 二进制 反序列化 Dictionary
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        public static T FluorineFxAMF3<T>(byte[] buf)
        {
            string JsonStr = "";
            object jsonObj=null;
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                using (System.IO.MemoryStream steram = new System.IO.MemoryStream(buf))
                {
                    FluorineFx.AMF3.ByteArray ba = new FluorineFx.AMF3.ByteArray(steram);
                    jsonObj = ba.ReadObject();
                }
            }
            timer.Stop();
            if (jsonObj is Dictionary<string, object>)
                JsonStr = JsonHelp.ModelToJavaScriptJson((T)jsonObj);
            else
                JsonStr = JsonHelp.ObjectToJson((T)jsonObj); 
            Console.WriteLine("内存使用:" + Help.BytesCountToString(Environment.WorkingSet) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return (T)jsonObj;
        }


        /// <summary>
        /// NET类库 BinaryFormatter 二进制 反序列化 Dictionary
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        public static T BinaryFormatterByte<T>(byte[] buf)
        {
            string JsonStr = "";
            object jsonObj = null;
            System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < forInt; i++)
            {
                using (System.IO.MemoryStream steram = new System.IO.MemoryStream(buf))
                {
                    jsonObj = formatter.Deserialize(steram);
                }
            }
            timer.Stop();
            if (jsonObj is Dictionary<string, object>)
                JsonStr = JsonHelp.ModelToJavaScriptJson((T)jsonObj);
            else
                JsonStr = JsonHelp.ObjectToJson((T)jsonObj); 
            Console.WriteLine("内存使用:" + Help.BytesCountToString(Environment.WorkingSet) + " 执行" + forInt + "次,用时:" + timer.ElapsedMilliseconds + "毫秒!");
            Console.WriteLine(JsonStr + Environment.NewLine);
            return (T)jsonObj;
        }

        /// <summary>
        /// 计算 存储单位
        /// </summary>
        /// <param name="len">
        public static string StringToSize(string str)
        {
            Byte[] byteArray = Encoding.UTF8.GetBytes(str);
            return Help.BytesToSize(byteArray);
        }
    }
}
